#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>

#define BLACK 0x00000000  // 黑色
#define RED 0x00ff0000    // 红色
#define GREEN 0x0000ff00  // 绿色
#define YELLOW 0x00ffff00 // 黄色
#define BLUE 0x000000ff   // 蓝色
#define PURPLE 0x00ff00ff // 紫色
#define CYAN 0x0000ffff   // 青色
#define WHITE 0x00ffffff  // 白色
#define GRAY 0x00808080   // 灰色
#define PINK 0x00ff8080   // 粉色
#define ORANGE 0x00ff8000 // 橙色

// 字母 N 的位图
const int letter_N[8][8] = {
    {1, 0, 0, 0, 0, 0, 0, 1},
    {1, 1, 0, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 0, 0, 1, 0, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 0, 1, 1},
    {1, 0, 0, 0, 0, 0, 0, 1}};

// 字母 A 的位图
const int letter_A[8][8] = {
    {0, 0, 1, 1, 1, 0, 0, 0},
    {0, 1, 0, 0, 0, 1, 0, 0},
    {1, 0, 0, 0, 0, 0, 1, 0},
    {1, 1, 1, 1, 1, 1, 1, 0},
    {1, 0, 0, 0, 0, 0, 1, 0},
    {1, 0, 0, 0, 0, 0, 1, 0},
    {1, 0, 0, 0, 0, 0, 1, 0},
    {1, 0, 0, 0, 0, 0, 1, 0}};

// 字母 I 的位图
const int letter_I[8][8] = {
    {0, 1, 1, 1, 1, 1, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0},
    {0, 1, 1, 1, 1, 1, 0, 0}};

// 字母 M 的位图
const int letter_M[8][8] = {
    {1, 0, 0, 0, 0, 0, 0, 1},
    {1, 1, 0, 0, 0, 0, 1, 1},
    {1, 0, 1, 0, 0, 1, 0, 1},
    {1, 0, 0, 1, 1, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 1}};

// 绘制字母函数
void draw_letter(int buf[], int start_x, int start_y, const int letter[8][8])
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (letter[i][j])
            {
                int pos = (start_y + i) * 800 + (start_x + j);
                buf[pos] = BLUE;
            }
        }
    }
}

int main()
{
    int fd;
    fd = open("/dev/fb0", O_RDWR);
    if (fd < 0)
    {
        printf("open /dev/fb0 failed error: %s\n", strerror(errno));
        return -1;
    }

    printf("open /dev/fb0 success\n");

    int buf[480 * 800] = {0}; // 初始化全黑

    // 绘制 NAIM
    draw_letter(buf, 100, 200, letter_N); // N
    draw_letter(buf, 118, 200, letter_A); // A (100 + 8 + 10)
    draw_letter(buf, 136, 200, letter_I); // I
    draw_letter(buf, 154, 200, letter_M); // M



    write(fd, buf, sizeof(buf));
    close(fd);

    return 0;
}